library(tidyverse)
library(lubridate)
library(ggplot2)
library(data.table)
library(readxl)
library(openxlsx)

# create output directory for plots if it doesn't already exist
# output_dir = "outputs" # name of output directory
# main_dir = getwd() # get directory of project


# output_dir <- file.path(main_dir, sub_dir) # Provide the dir name(ex: sub dir) that you want to create under main dir
# if (!dir.exists(output_dir)){
#   dir.create(output_dir)
# } else {
#   print("Directory already exists!")
# }


# P1 effect size  ------------------------------------------------------

# added verbose for silent
p1 <- read_excel("R Figures Data.xlsx", sheet = "p1_imot_plotting")
p1<-as_tibble(p1)
p1$IV <- factor(p1$IV,levels=p1$IV)

ppi <- 1000
tiff("outputs/p1_effect_size_imot.tiff", width=10*ppi, height=6*ppi, res=ppi)

# errorbar plots
ggplot(p1, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=3) +   # points (beta)
  xlab("Relationship with Intrinsic Motivation") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.1, 0.6) + # x and y axis limits
  geom_vline(xintercept = 0, linetype="dotted")+
  theme_classic()
dev.off()

#ggsave("outputs/p1_effect_size_imot.tiff", height = 5, width = 10, dpi = 1200)




# P2 effect size (Figure 2) ------------------------------------------------------

# added verbose for silent
p2 <- read_excel("data/Data.xlsx", sheet = "p2_imot_plotting")
p2<-as_tibble(p2)
p2$IV <- factor(p2$IV,levels=p2$IV)


# errorbar plots
tiff("outputs/figure2_p2_effect_size_imot.tiff", width=10*ppi, height=5*ppi, res=ppi)

ggplot(p2, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=3) +   # points (beta)
  xlab("Relationship with Intrinsic Motivation") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.1, 0.6) + # x and y axis limits
  geom_vline(xintercept = 0, linetype="dotted")+
  facet_grid(rows = vars(group_code),
             scales = "free_y",
             space = "free_y",
             switch = "y") +
  theme_classic()
dev.off()

#ggsave("outputs/figure2_p2_effect_size_imot.tiff", height = 5, width = 10, dpi = 320)



# P3 effect size (Figure 1) ------------------------------------------------------

# added verbose for silent
p3 <- read_excel("data/Data.xlsx", sheet = "p3_imot_plotting")
p3<-as_tibble(p3)
p3$IV <- factor(p3$IV,levels=p3$IV)


# errorbar plots
tiff("outputs/figure1_p3_effect_size_imot.tiff", width=10*ppi, height=5*ppi, res=ppi)

ggplot(p3, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=1) +   # points (beta)
  xlab("Relationship with Intrinsic Motivation") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.1, 0.6) + # x and y axis limits
  geom_vline(xintercept = 0, linetype="dotted")+
  facet_grid(rows = vars(group_code),
             scales = "free_y",
             space = "free_y",
             switch = "y") +
  theme_classic()
dev.off()
#ggsave("outputs/figure1_p3_effect_size_imot.tiff", height = 5, width = 10, dpi = 320)




# P3 leave interact (Figure 3) ------------------------------------------------------

# added verbose for silent
p3_leave_interact <- read_excel("data/Data.xlsx", sheet = "p3_leave_hetro_plotting_interac")
p3_leave_interact<-as_tibble(p3_leave_interact)
p3_leave_interact$IV <- factor(p3_leave_interact$IV,levels=p3_leave_interact$IV)


# errorbar plots
tiff("outputs/figure3_p3_leave_interact_effect_size_imot.tiff", width=10*ppi, height=5*ppi, res=ppi)
ggplot(p3_leave_interact, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=1) +   # points (beta)
  xlab("Relationship with Intent to Leave") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.2, 0.05) + # x and y axis limits
  geom_vline(xintercept = 0, linetype="dotted")+
  facet_grid(rows = vars(group_code),
             scales = "free_y",
             space = "free_y",
             switch = "y") +
  theme_classic()
dev.off()
#ggsave("outputs/figure3_p3_leave_interact_effect_size_imot.tiff", height = 5, width = 10, dpi = 320)



# h4_p2 non_ver interact (Figure 5) ------------------------------------------------------

# added verbose for silent
h4_p2 <- read_excel("data/Data.xlsx", sheet = "h4_p2_plotting")
h4_p2<-as_tibble(h4_p2)
h4_p2$IV <- factor(h4_p2$IV,levels=h4_p2$IV)

# errorbar plots
tiff("outputs/figure5_h4_p2_effect_size_imot.tiff", width=10*ppi, height=5*ppi, res=ppi)
ggplot(h4_p2, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=3) +   # points (beta)
  xlab("Relationship with Intrinsic Motivation") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.3, 0.5) + # x and y axis limits
  geom_vline(xintercept = 0, linetype="dotted")+
  facet_grid(rows = vars(group_code),
             scales = "free_y",
             space = "free_y",
             switch = "y") +
  theme_classic()
dev.off()
#ggsave("outputs/figure5_h4_p2_effect_size_imot.tiff", height = 5, width = 10, dpi = 320)


# h4_p1 non_ver interact (Figure 4) ---------------------------------------------

# added verbose for silent
h4_p1 <- read_excel("data/Data.xlsx", sheet = "h4_p1_plotting")
h4_p1<-as_tibble(h4_p1)
h4_p1$IV <- factor(h4_p1$IV,levels=h4_p1$IV)

# errorbar plots
tiff("outputs/figure4_h4_p1_effect_size_imot.tiff", width=10*ppi, height=5*ppi, res=ppi)
ggplot(h4_p1, aes(x=beta_full, y=IV)) +  # base
  geom_errorbar(aes(xmin=beta_full-ME_Full, xmax=beta_full+ME_Full),
                colour="black", width=0.5) +  # error bar
  geom_point(size=3) +   # points (beta)
  xlab("Relationship with Intrinsic Motivation") +      
  ylab("Management Practices") +  # x and y axis labels
  xlim(-0.3, 0.5) + # x and y axis limits
  facet_grid(rows = vars(group_code),
             scales = "free_y",
             space = "free_y",
             switch = "y") +
  geom_vline(xintercept = 0, linetype="dotted")+
  theme_classic()
dev.off()
#ggsave("outputs/figure4_h4_p1_effect_size_imot.tiff", height = 5, width = 10, dpi = 320)



